2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_VIEWPORTHANDLER_JUCEHEADER__
27 #define __JUCER_VIEWPORTHANDLER_JUCEHEADER__
30 //==============================================================================
33 class ViewportHandler
: public ComponentTypeHandler
36 //==============================================================================
38 : ComponentTypeHandler ("Viewport", "Viewport", typeid (UpdatingViewport
), 150, 150)
41 //==============================================================================
42 Component
* createNewComponent (JucerDocument
*)
44 Viewport
* const v
= new UpdatingViewport ("new viewport");
45 v
->setViewedComponent (new ViewportDemoContentComp());
50 XmlElement
* createXmlFor (Component
* comp
, const ComponentLayout
* layout
)
52 Viewport
* const v
= dynamic_cast <Viewport
*> (comp
);
53 XmlElement
* const e
= ComponentTypeHandler::createXmlFor (comp
, layout
);
55 e
->setAttribute ("vscroll", v
->isVerticalScrollBarShown());
56 e
->setAttribute ("hscroll", v
->isHorizontalScrollBarShown());
57 e
->setAttribute ("scrollbarThickness", v
->getScrollBarThickness());
59 e
->setAttribute ("contentType", getViewportContentType (v
));
60 e
->setAttribute ("jucerFile", getViewportJucerComponentFile (v
));
61 e
->setAttribute ("contentClass", getViewportGenericComponentClass (v
));
62 e
->setAttribute ("constructorParams", getViewportConstructorParams (v
));
67 bool restoreFromXml (const XmlElement
& xml
, Component
* comp
, const ComponentLayout
* layout
)
69 if (! ComponentTypeHandler::restoreFromXml (xml
, comp
, layout
))
72 Viewport defaultViewport
;
73 Viewport
* const v
= dynamic_cast <Viewport
*> (comp
);
74 v
->setScrollBarsShown (xml
.getBoolAttribute ("vscroll", defaultViewport
.isVerticalScrollBarShown()),
75 xml
.getBoolAttribute ("hscroll", defaultViewport
.isHorizontalScrollBarShown()));
77 v
->setScrollBarThickness (xml
.getIntAttribute ("scrollbarThickness", defaultViewport
.getScrollBarThickness()));
79 setViewportJucerComponentFile (v
, xml
.getStringAttribute ("jucerFile", String::empty
));
80 setViewportGenericComponentClass (v
, xml
.getStringAttribute ("contentClass"));
81 setViewportContentType (v
, xml
.getIntAttribute ("contentType", 0));
82 setViewportConstructorParams (v
, xml
.getStringAttribute ("constructorParams"));
87 void getEditableProperties (Component
* component
, JucerDocument
& document
, Array
<PropertyComponent
*>& properties
)
89 ComponentTypeHandler::getEditableProperties (component
, document
, properties
);
91 Viewport
* const v
= dynamic_cast <Viewport
*> (component
);
93 properties
.add (new ViewportScrollbarShownProperty (v
, document
, true));
94 properties
.add (new ViewportScrollbarShownProperty (v
, document
, false));
95 properties
.add (new ViewportScrollbarSizeProperty (v
, document
));
96 properties
.add (new ViewportContentTypeProperty (v
, document
));
98 if (getViewportContentType (v
) == 1)
100 properties
.add (new ViewportJucerFileProperty (v
, document
));
101 properties
.add (new ConstructorParamsProperty (v
, document
));
103 else if (getViewportContentType (v
) == 2)
105 properties
.add (new ViewportContentClassProperty (v
, document
));
106 properties
.add (new ConstructorParamsProperty (v
, document
));
110 const String
getCreationParameters (Component
* comp
)
112 return quotedString (comp
->getName());
115 void fillInCreationCode (GeneratedCode
& code
, Component
* component
, const String
& memberVariableName
)
117 Viewport defaultViewport
;
118 Viewport
* const v
= dynamic_cast <Viewport
*> (component
);
120 ComponentTypeHandler::fillInCreationCode (code
, component
, memberVariableName
);
122 if (defaultViewport
.isVerticalScrollBarShown() != v
->isVerticalScrollBarShown()
123 || defaultViewport
.isHorizontalScrollBarShown() != v
->isHorizontalScrollBarShown())
126 << memberVariableName
<< "->setScrollBarsShown ("
127 << boolToString (v
->isVerticalScrollBarShown()) << ", "
128 << boolToString (v
->isHorizontalScrollBarShown()) << ");\n";
131 if (defaultViewport
.getScrollBarThickness() != v
->getScrollBarThickness())
134 << memberVariableName
<< "->setScrollBarThickness ("
135 << v
->getScrollBarThickness() << ");\n";
138 if (getViewportContentType (v
) != 0)
140 String
className (getViewportGenericComponentClass (v
));
142 if (getViewportContentType (v
) == 1)
146 const String
filename (getViewportJucerComponentFile (v
));
148 if (filename
.isNotEmpty())
149 file
= code
.document
->getFile().getSiblingFile (filename
);
151 JucerDocument
* doc
= ObjectTypes::loadDocumentFromFile (file
, false);
155 code
.includeFilesCPP
.add (doc
->getFile().withFileExtension ("h")
156 .getRelativePathFrom (code
.document
->getFile().getParentDirectory())
157 .replaceCharacter ('\\', '/'));
159 className
= doc
->getClassName();
164 className
= String::empty
;
168 if (className
.isNotEmpty())
171 << memberVariableName
<< "->setViewedComponent (new "
174 if (getViewportConstructorParams (v
).trim().isNotEmpty())
176 code
.constructorCode
<< " (" << getViewportConstructorParams (v
).trim() << "));\n";
180 code
.constructorCode
<< "());\n";
185 code
.constructorCode
<< "\n";
188 static void updateViewportContentComp (Viewport
* vp
)
190 if (getViewportContentType (vp
) == 1)
192 JucerDocument
* doc
= findParentDocument (vp
);
193 TestComponent
* tc
= new TestComponent (doc
, 0, false);
195 tc
->setFilename (getViewportJucerComponentFile (vp
));
196 tc
->setToInitialSize();
198 vp
->setViewedComponent (tc
);
202 vp
->setViewedComponent (new ViewportDemoContentComp());
206 static int getViewportContentType (Viewport
* vp
)
208 return vp
->getProperties() ["contentType"];
211 static void setViewportContentType (Viewport
* vp
, int newValue
)
213 if (newValue
!= getViewportContentType (vp
))
215 vp
->getProperties().set ("contentType", newValue
);
216 updateViewportContentComp (vp
);
220 static const String
getViewportJucerComponentFile (Viewport
* vp
)
222 return vp
->getProperties() ["jucerFile"].toString();
225 static void setViewportJucerComponentFile (Viewport
* vp
, const String
& file
)
227 if (file
!= getViewportJucerComponentFile (vp
))
229 vp
->getProperties().set ("jucerFile", file
);
230 updateViewportContentComp (vp
);
234 static const String
getViewportGenericComponentClass (Viewport
* vp
)
236 return vp
->getProperties() ["contentClass"].toString();
239 static void setViewportGenericComponentClass (Viewport
* vp
, const String
& className
)
241 if (className
!= getViewportGenericComponentClass (vp
))
243 vp
->getProperties().set ("contentClass", className
);
244 updateViewportContentComp (vp
);
248 static const String
getViewportConstructorParams (Viewport
* vp
)
250 return vp
->getProperties() ["constructorParams"].toString();
253 static void setViewportConstructorParams (Viewport
* vp
, const String
& newParams
)
255 if (newParams
!= getViewportConstructorParams (vp
))
257 vp
->getProperties().set ("constructorParams", newParams
);
258 updateViewportContentComp (vp
);
263 //==============================================================================
264 class UpdatingViewport
: public Viewport
267 UpdatingViewport (const String
& name
)
272 void parentHierarchyChanged()
274 Viewport::parentHierarchyChanged();
275 updateViewportContentComp (this);
279 //==============================================================================
280 class ViewportDemoContentComp
: public Component
283 ViewportDemoContentComp()
285 setSize (2048, 2048);
288 void paint (Graphics
& g
)
290 g
.fillCheckerBoard (getLocalBounds(), 50, 50,
291 Colours::lightgrey
.withAlpha (0.5f
),
292 Colours::darkgrey
.withAlpha (0.5f
));
296 //==============================================================================
297 class ViewportScrollbarShownProperty
: public ComponentBooleanProperty
<Viewport
>
300 ViewportScrollbarShownProperty (Viewport
* comp
, JucerDocument
& document
, const bool vertical_
)
301 : ComponentBooleanProperty
<Viewport
> (vertical_
? "V scrollbar" : "H scrollbar",
302 "enabled", "enabled",
308 void setState (bool newState
)
310 document
.perform (new ViewportScrollbarChangeAction (component
, *document
.getComponentLayout(), vertical
, newState
),
311 "Change Viewport scrollbar");
314 bool getState() const
316 return vertical
? component
->isVerticalScrollBarShown()
317 : component
->isHorizontalScrollBarShown();
323 class ViewportScrollbarChangeAction
: public ComponentUndoableAction
<Viewport
>
326 ViewportScrollbarChangeAction (Viewport
* const comp
, ComponentLayout
& layout
, const bool vertical_
, const bool newState_
)
327 : ComponentUndoableAction
<Viewport
> (comp
, layout
),
328 vertical (vertical_
),
331 oldState
= vertical
? comp
->isVerticalScrollBarShown()
332 : comp
->isHorizontalScrollBarShown();
339 getComponent()->setScrollBarsShown (newState
, getComponent()->isHorizontalScrollBarShown());
341 getComponent()->setScrollBarsShown (getComponent()->isVerticalScrollBarShown(), newState
);
351 getComponent()->setScrollBarsShown (oldState
, getComponent()->isHorizontalScrollBarShown());
353 getComponent()->setScrollBarsShown (getComponent()->isVerticalScrollBarShown(), oldState
);
358 bool vertical
, newState
, oldState
;
362 //==============================================================================
363 class ViewportScrollbarSizeProperty
: public SliderPropertyComponent
,
364 public ChangeListener
367 ViewportScrollbarSizeProperty (Viewport
* comp
, JucerDocument
& document_
)
368 : SliderPropertyComponent ("scrollbar size", 3.0, 30.0, 1.0, 1.0),
372 document
.addChangeListener (this);
375 ~ViewportScrollbarSizeProperty()
377 document
.removeChangeListener (this);
380 void setValue (double newValue
)
382 document
.getUndoManager().undoCurrentTransactionOnly();
384 document
.perform (new ViewportScrollbarSizeChangeAction (component
, *document
.getComponentLayout(), roundToInt (newValue
)),
385 "Change Viewport scrollbar size");
388 double getValue() const
390 return component
->getScrollBarThickness();
393 void changeListenerCallback (ChangeBroadcaster
*)
399 JucerDocument
& document
;
402 class ViewportScrollbarSizeChangeAction
: public ComponentUndoableAction
<Viewport
>
405 ViewportScrollbarSizeChangeAction (Viewport
* const comp
, ComponentLayout
& layout
, const int newState_
)
406 : ComponentUndoableAction
<Viewport
> (comp
, layout
),
409 oldState
= comp
->getScrollBarThickness();
415 getComponent()->setScrollBarThickness (newState
);
423 getComponent()->setScrollBarThickness (newState
);
428 int newState
, oldState
;
432 //==============================================================================
433 class ViewportContentTypeProperty
: public ComponentChoiceProperty
<Viewport
>
436 ViewportContentTypeProperty (Viewport
* comp
, JucerDocument
& document
)
437 : ComponentChoiceProperty
<Viewport
> ("content", comp
, document
)
439 choices
.add ("No content component");
440 choices
.add ("Jucer content component");
441 choices
.add ("Named content component");
444 void setIndex (int newIndex
)
446 document
.perform (new ViewportContentTypeChangeAction (component
, *document
.getComponentLayout(), newIndex
),
447 "Change Viewport content type");
452 return getViewportContentType (component
);
456 class ViewportContentTypeChangeAction
: public ComponentUndoableAction
<Viewport
>
459 ViewportContentTypeChangeAction (Viewport
* const comp
, ComponentLayout
& layout
, const int newValue_
)
460 : ComponentUndoableAction
<Viewport
> (comp
, layout
),
463 oldValue
= getViewportContentType (comp
);
469 setViewportContentType (getComponent(), newValue
);
471 layout
.getDocument()->refreshAllPropertyComps();
478 setViewportContentType (getComponent(), oldValue
);
480 layout
.getDocument()->refreshAllPropertyComps();
484 int newValue
, oldValue
;
488 //==============================================================================
489 class ViewportJucerFileProperty
: public FilePropertyComponent
,
490 public ChangeListener
493 ViewportJucerFileProperty (Viewport
* const component_
, JucerDocument
& document_
)
494 : FilePropertyComponent ("Jucer file", false, true),
495 component (component_
),
498 document
.addChangeListener (this);
501 ~ViewportJucerFileProperty()
503 document
.removeChangeListener (this);
506 //==============================================================================
507 void setFile (const File
& newFile
)
509 document
.perform (new JucerCompFileChangeAction (component
, *document
.getComponentLayout(),
510 newFile
.getRelativePathFrom (document
.getFile().getParentDirectory())
511 .replaceCharacter ('\\', '/')
513 "Change Jucer component file");
516 const File
getFile() const
518 const String
filename (getViewportJucerComponentFile (component
));
520 if (filename
.isEmpty())
521 return File::nonexistent
;
523 return document
.getFile().getSiblingFile (filename
);
526 void changeListenerCallback (ChangeBroadcaster
*)
532 Viewport
* const component
;
533 JucerDocument
& document
;
535 class JucerCompFileChangeAction
: public ComponentUndoableAction
<Viewport
>
538 JucerCompFileChangeAction (Viewport
* const comp
, ComponentLayout
& layout
, const String
& newState_
)
539 : ComponentUndoableAction
<Viewport
> (comp
, layout
),
542 oldState
= getViewportJucerComponentFile (comp
);
548 setViewportJucerComponentFile (getComponent(), newState
);
556 setViewportJucerComponentFile (getComponent(), oldState
);
561 String newState
, oldState
;
565 //==============================================================================
566 class ViewportContentClassProperty
: public ComponentTextProperty
<Viewport
>
569 ViewportContentClassProperty (Viewport
* comp
, JucerDocument
& document
)
570 : ComponentTextProperty
<Viewport
> ("content class", 256, false, comp
, document
)
574 void setText (const String
& newText
)
576 document
.perform (new ViewportClassNameChangeAction (component
, *document
.getComponentLayout(), newText
),
577 "Change Viewport content class");
580 const String
getText() const
582 return getViewportGenericComponentClass (component
);
586 class ViewportClassNameChangeAction
: public ComponentUndoableAction
<Viewport
>
589 ViewportClassNameChangeAction (Viewport
* const comp
, ComponentLayout
& layout
, const String
& newValue_
)
590 : ComponentUndoableAction
<Viewport
> (comp
, layout
),
593 oldValue
= getViewportGenericComponentClass (comp
);
599 setViewportGenericComponentClass (getComponent(), newValue
);
601 layout
.getDocument()->refreshAllPropertyComps();
608 setViewportGenericComponentClass (getComponent(), oldValue
);
610 layout
.getDocument()->refreshAllPropertyComps();
614 String newValue
, oldValue
;
618 //==============================================================================
619 class ConstructorParamsProperty
: public ComponentTextProperty
<Viewport
>
622 ConstructorParamsProperty (Viewport
* comp
, JucerDocument
& document
)
623 : ComponentTextProperty
<Viewport
> ("constructor params", 512, false, comp
, document
)
627 void setText (const String
& newText
)
629 document
.perform (new ConstructorParamChangeAction (component
, *document
.getComponentLayout(), newText
),
630 "Change Viewport content constructor params");
633 const String
getText() const
635 return getViewportConstructorParams (component
);
641 class ConstructorParamChangeAction
: public ComponentUndoableAction
<Viewport
>
644 ConstructorParamChangeAction (Viewport
* const comp
, ComponentLayout
& layout
, const String
& newValue_
)
645 : ComponentUndoableAction
<Viewport
> (comp
, layout
),
648 oldValue
= getViewportConstructorParams (comp
);
654 setViewportConstructorParams (getComponent(), newValue
);
656 layout
.getDocument()->refreshAllPropertyComps();
663 setViewportConstructorParams (getComponent(), oldValue
);
665 layout
.getDocument()->refreshAllPropertyComps();
669 String newValue
, oldValue
;
675 #endif // __JUCER_VIEWPORTHANDLER_JUCEHEADER__